package com.shuke.medical.insure.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Random;

/***
 * @title SignUtil
 * @description
 * @author woo hua
 * @version 1.0.0
 * @create 2023/3/22 16:46
 **/
public class SignUtil {

    private SignUtil() {
    }

    /**
     * 1.	x-tif-paasid	应用编码	字符型	30		Y	在创建账户时自动生成应用编码和密钥（secretKey私钥）。
     * 2.	x-tif-signature	签名	字符型	100		Y	Sha256加密生成的签名。x-tif-signature = sha256(x-tif-timestamp + secretKey+ x-tif-nonce + x-tif-timestamp)
     * 3.	x-tif-timestamp	时间	时间戳			Y	当前 unix 时间戳（秒）。
     * 4.	x-tif-nonce	校验码	字符型	100		Y	非重复的随机字符串（并非定长100位，十分钟内不能重复）。
     * @param args
     */
    public static void main(String[] args) {
        // 非重复的随机字符串（并非定长100位，十分钟内不能重复）。
        String xRioNonce = getRandomString(32);
        // 当前 unix 时间戳（秒）。
        String xRioTimestamp = String.valueOf(getCurrentUnixSeconds());
        String secretKey = "abcdefg";
        // Sha256加密生成的签名。x-tif-signature = sha256(x-tif-timestamp + secretKey+ x-tif-nonce + xtif-timestamp)
        String signature = getSHA256Str(xRioTimestamp + secretKey + xRioNonce + xRioTimestamp);
        System.out.println("==============请求头参数=================");
        System.out.println("x-tif-signature:" + signature);
        System.out.println("x-tif-timestamp:" + xRioTimestamp);
        System.out.println("x-tif-nonce:" + xRioNonce);

        System.out.println("===============请求头参数长度================");
        System.out.println("x-tif-signature length:" + signature.length());
        System.out.println("x-tif-timestamp length:" + xRioTimestamp.length());
        System.out.println("x-tif-nonce length:" + xRioNonce.length());

        System.out.println("================示例 请求头参数长度===============");
        System.out.println("x-tif-signature length:" + "DP6XaaA5CQGX487d9zayRgxVlmuoFOO7fvmYRc".length());
        System.out.println("x-tif-timestamp length:" + "1583746499812".length());
        System.out.println("x-tif-nonce length:" + "lxXEn9u9uVDtUybpelZqru0qyao".length());
    }

    /*** 获取随机数 */
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; ++i) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /*** 获取当前时间戳 */
    public static long getCurrentUnixSeconds() {
        ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.toEpochSecond(zoneOffset);
    }

    /*** 使用SHA-256算法进行加密 */
    public static String getSHA256Str(String str) {
        String encodeStr = "";
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException var4) {
            var4.printStackTrace();
        } catch (UnsupportedEncodingException var5) {
            var5.printStackTrace();
        }
        return encodeStr;
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; ++i) {
            temp = Integer.toHexString(bytes[i] & 255);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}
